﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;


namespace FYPDemo
{
    public partial class WebForm10 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
            if (!IsPostBack)
            {
                string name = Session["username"].ToString();
                string firstCheckCart = "";
                string secondCheckCart = "";
                DataTable dt1 = ProductManagement.getLatestCart(name);

                if (dt1.Rows.Count > 0)
                {
                    firstCheckCart = dt1.Rows[0][0].ToString();
                }

                ProductManagement.cartManagement();

                if (firstCheckCart != "")
                {
                    DataTable dt = ProductManagement.getLatestCart(name);

                    if (dt.Rows.Count > 0)
                    {
                        secondCheckCart = dt.Rows[0][0].ToString();
                    }

                    if (firstCheckCart != secondCheckCart)
                    {
                        MessageBox.Show("Your cart being cancel because it has expired.");
                    }

                    else
                    {
                        ListView1.DataSource = dt;
                        ListView1.DataBind();
                    }
                }
            }
        }

        protected void ListView1_DataBound(object sender, EventArgs e)
        {
            if (!computeTotal())
                btnCheckout.Enabled = false;
            else
                btnCheckout.Enabled = true;
        }

        private bool computeTotal()
        {
            decimal total = 0.0M;
            foreach (ListViewDataItem item in ListView1.Items)
            {
                Label subTotalLabel = (Label)item.FindControl("SubTotalLabel");
                if (subTotalLabel != null)
                {
                    total += Convert.ToDecimal(subTotalLabel.Text);
                }
            }

            lblTotal.Text = "RM" + total;

            return total == 0.0M ? false : true;
        }

        protected void btnCheckout_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            DataTable dt1 = new DataTable();
            string name = Session["username"].ToString();
            string latestCart = null;
            int result = 0;
            double TotalPrice = 0;

            dt = ProductManagement.getLatestCart(name);

            if (dt.Rows.Count > 0)
            {

                latestCart = dt.Rows[0][0].ToString();
            }

            if (latestCart != "")
            {
                result = ProductManagement.checkOutCart(latestCart);

                if (result > 0)
                {
                    dt1 = ProductManagement.getSubTotal(latestCart);

                    if (dt1.Rows.Count > 0)
                    {
                        foreach (DataRow dtRows in dt1.Rows)
                        {
                            TotalPrice += Convert.ToDouble(dtRows[0]);
                        }
                    }

                    ProductManagement.UpdateOrderTotalPrice(latestCart, TotalPrice);
                    Response.Redirect("OrdersSummary.aspx");
                }

                else
                {
                    MessageBox.Show("There are some errors occur during checkout process.");
                }
            }

            else
            {
                //no cart to check out
            }
                
        }

        
    }
}